﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuyennvLibrary.Enum;

namespace TuyennvLibrary.Attributes
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
    public sealed class AttributeDescriptionMethod : Attribute, IComparable<AttributeDescriptionMethod>
    {
        public static int Method = 1;
        public static int Param = 2;
        public static int Return = 3;
        /// <summary>
        /// Các kiểu cho mô tả thuộc giá trị 1,2,3: Ví dụ AttributeMethod.Method=1;
        /// </summary>
        public int TypeDescription { get; set; }
        public List<MethodDictionary> ListReturn { get; set; }
        public List<MethodDictionary> ListParam { get; set; }
        public List<MethodDictionary> ListMethod { get; set; }
        /// <summary>
        /// Mô tả có thể dùng các thẻ html để hiển thị cho tốt
        /// </summary>
        public string Description { get; set; }
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription, string Description)
        {            
            this.TypeDescription = (int)TypeDescription;
            this.Description = Description;
            this.ListReturn = new List<MethodDictionary>();
            this.ListParam = new List<MethodDictionary>();
            this.ListMethod = new List<MethodDictionary>();
        }
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription,string field, string Description):this(TypeDescription,"<strong>" + field + "</strong>: " + Description)
        {
        }
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription, string title, string[] Values, string[] Contents)
        {
            this.TypeDescription = (int)TypeDescription;
            this.ListReturn = new List<MethodDictionary>();
            this.ListParam = new List<MethodDictionary>();
            this.ListMethod = new List<MethodDictionary>();
            for (int i = 0; i < Values.Length; i++)
            {
                if (this.TypeDescription == Return)
                {
                    ListReturn.Add(new MethodDictionary { Name = Values[i], Content = Contents[i] });
                }
                else if (this.TypeDescription == Param)
                {
                    ListParam.Add(new MethodDictionary { Name = Values[i], Content = Contents[i] });
                }
                else if (this.TypeDescription == Method)
                {
                    ListMethod.Add(new MethodDictionary { Name = Values[i], Content = Contents[i] });
                }
            }
            this.Description = title;
        }
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription,string field, string title, string[] Values, string[] Contents):
            this(TypeDescription,"<strong>" + field + "</strong>: " + title,Values,Contents)
        {
        }
        /// <summary>
        /// Mô tả với nhiều giá trị
        /// </summary>
        /// <param name="TypeDescription">Loại mô tả</param>
        /// <param name="title">Tiêu đề thông tin</param>
        /// <param name="Values">Các giá trị cách nhau bởi dấu |</param>
        /// <param name="Contents">Các giá trị cách nhau bởi dấu |</param>
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription, string title, string Values, string Contents)
            :this(TypeDescription, title, Values.Split('|'), Contents.Split('|'))
        {
            
        }
        public AttributeDescriptionMethod(TypeDescriptionMethod TypeDescription,string field, string title, string Values, string Contents)
            : this(TypeDescription, "<strong>" + field + "</strong>: " + title, Values.Split('|'), Contents.Split('|'))
        {

        }
        public string getDescription(AttributeDescriptionMethod attribute)
        {
            return attribute.ToString();
        }
        public override bool Equals(object obj)
        {
            if (((AttributeDescriptionMethod)obj).TypeDescription == Return)
                return false;
            if (this.TypeDescription == ((AttributeDescriptionMethod)obj).TypeDescription)
                return true;
            return false;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            if (this.TypeDescription == AttributeDescriptionMethod.Method)
            {
                string content = "<i style=\"color:Red\">Method</i>: " + this.Description + "<br />";
                foreach (MethodDictionary item in ListMethod)
                {
                    content += string.Format("<i style=\"color:Blue\"><strong>{0}</strong></i>: {1}<br />", item.Name, item.Content);

                }
                return content;
            }
            if (this.TypeDescription == AttributeDescriptionMethod.Param)
            {
                string content = "<i style=\"color:Red\">Param</i>: " + this.Description + "<br />";
                foreach (MethodDictionary item in ListParam)
                {
                    content += string.Format("<i style=\"color:Blue\"><strong>{0}</strong></i>: {1}<br />", item.Name, item.Content);

                }
                return content;
            }
            if (this.TypeDescription == AttributeDescriptionMethod.Return)
            {
                string content= "<i style=\"color:Red\">Return</i>: " + this.Description+"<br />";
                foreach (MethodDictionary item in ListReturn)
                {
                    content += string.Format("<i style=\"color:Blue\"><strong>{0}</strong></i>: {1}<br />", item.Name, item.Content);

                }
                return content;
            }
            return string.Empty;
        }
        public int CompareTo(AttributeDescriptionMethod other)
        {
            if (other == null)
                return 1;

            else
            {
                return this.TypeDescription.CompareTo(other.TypeDescription);

            }
        }
        
    }
    public class MethodDictionary
    {
        public string Name { get; set; }
        public string Content { get; set; }
    }
}
